package com.hgd.evp.common.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hgd.evp.common.entity.Query;
import com.hgd.evp.common.mapper.CommonMapper;
import com.hgd.evp.common.utils.EmptyUtil;
import com.hgd.evp.common.utils.TableResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

public abstract class BaseBiz<M extends CommonMapper<T>, T> {
	@Autowired
	protected M mapper;

	public void setMapper(M mapper) {
		this.mapper = mapper;
	}

	public T selectOne(T entity) {
		return mapper.selectOne(entity);
	}

	public T selectById(Object id) {
		return mapper.selectByPrimaryKey(id);
	}

	/**
	 * 选择性按ID查询
	 * 
	 * @param id
	 *            实例ID
	 * @param clazz
	 * @param arg0
	 *            待查询的字段
	 * @return
	 */
	public T selectPropertiesById(Object id, Class<T> clazz, String... arg0) {
		Example example = new Example(clazz).selectProperties(arg0);
		example.createCriteria().andEqualTo("id", id);
		List<T> instanceList = this.selectByExample(example);
		if (EmptyUtil.isNotEmpty(instanceList)) {
			return instanceList.get(0);
		}
		return null;
	}

	/**
	 * 选择性按ID查询<br/>
	 * 该方法可指定记录ID的字段名称
	 * 
	 * @param id
	 *            实例ID
	 * @param idField
	 *            ID对应的字段名称，如果ID字段就是"id",可直接使用
	 *            com.hgd.provider.commom.biz.BaseBiz.selectProperties(Object,
	 *            Class<T>, String...)
	 * @param clazz
	 * @param arg0
	 *            待查询的字段
	 * @return
	 */
	public T selectPropertiesById(Object id, String idField, Class<T> clazz, String... arg0) {
		Example example = new Example(clazz).selectProperties(arg0);
		example.createCriteria().andEqualTo(idField, id);
		List<T> instanceList = this.selectByExample(example);
		if (EmptyUtil.isNotEmpty(instanceList)) {
			return instanceList.get(0);
		}
		return null;
	}

	/**
	 * 查询排除arg0后的字段
	 * 
	 * @param id
	 *            实例ID
	 * @param clazz
	 * @param arg0
	 *            待排除的字段
	 * @return
	 */
	public T excludePropertiesById(Object id, Class<T> clazz, String... arg0) {
		Example example = new Example(clazz).excludeProperties(arg0);
		example.createCriteria().andEqualTo("id", id);
		List<T> instanceList = this.selectByExample(example);
		if (EmptyUtil.isNotEmpty(instanceList)) {
			return instanceList.get(0);
		}
		return null;
	}

	/**
	 * 查询排除arg0后的字段<br/>
	 * 该方法可指定记录ID的字段名称
	 * 
	 * @param id
	 *            实例ID
	 * @param idField ID对应的字段名称，如果ID字段就是"id",可直接使用
	 * com.hgd.provider.commom.biz.BaseBiz.excludeProperties(Object, Class<T>, String...)
	 * @param clazz
	 * @param arg0
	 *            待排除的字段
	 * @return
	 */
	public T excludePropertiesById(Object id, String idField, Class<T> clazz, String... arg0) {
		Example example = new Example(clazz).excludeProperties(arg0);
		example.createCriteria().andEqualTo(idField, id);
		List<T> instanceList = this.selectByExample(example);
		if (EmptyUtil.isNotEmpty(instanceList)) {
			return instanceList.get(0);
		}
		return null;
	}

	public List<T> selectList(T entity) {
		return mapper.select(entity);
	}

	public List<T> selectListAll() {
		return mapper.selectAll();
	}

	public Long selectCount(T entity) {
		return new Long(mapper.selectCount(entity));
	}

	public void insertSelective(T entity) {
		mapper.insertSelective(entity);
	}

	public void delete(T entity) {
		mapper.delete(entity);
	}

	public void deleteById(Object id) {
		mapper.deleteByPrimaryKey(id);
	}

	public void updateById(T entity) {
		mapper.updateByPrimaryKey(entity);
	}

	public void updateSelectiveById(T entity) {
		mapper.updateByPrimaryKeySelective(entity);

	}

	public List<T> selectByExample(Object example) {
		return mapper.selectByExample(example);
	}

	public int selectCountByExample(Object example) {
		return mapper.selectCountByExample(example);
	}

	public TableResultResponse<T> selectByQuery(Query query) {
		@SuppressWarnings("unchecked")
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
		Example example = new Example(clazz);
		query2criteria(query, example);
		Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
		List<T> list = this.selectByExample(example);
		return new TableResultResponse<T>(result.getTotal(), list);
	}

	public void query2criteria(Query query, Example example) {
		if (query.entrySet().size() > 0) {
			Example.Criteria criteria = example.createCriteria();
			for (Map.Entry<String, Object> entry : query.entrySet()) {
				criteria.andEqualTo(entry.getKey(), "%" + entry.getValue().toString() + "%");
			}
		}
	}

	/**
	 * 插入记录
	 * 该方法会检测entity中是否含有crtTime,crtUserId,crtUserName,crtTenantId属性
	 * 如果有，且属性为空，则向属性中注入默认值
	 * @param entity 待插入的实体
	 */
	public abstract void insertSelectiveBindCrt(T entity);

	/**
	 * 更新记录
	 * 该方法会检测entity中是否含有updTime,updUserId,updUserName属性
	 * 如果有，且属性为空，则向属性中注入默认值
	 * @param entity 待更新的实体
	 */
	public abstract void updateSelectiveByIdBindUpd(T entity);
}
